﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Aop.Api;
using Aop.Api.Request;
using LoginSdk.Model;
using LoginSdk.Utils;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using WqLunTan.Models;
using Newtonsoft.Json;
using WqLunTan.DBContext;
using WqLunTan.Models.ViewModel;
using Common;
using WqLunTan.Models.DataBase;

namespace WqLunTan.Controllers
{
    public class HomeController : BaseController
    {
        private readonly MySqlDbContext dbContext;

        public HomeController(MySqlDbContext _dbContext)
        {
            dbContext = _dbContext;
        }

        public ActionResult Index()
        {
            try
            {
                var hotKey = $"home-index-hot";
                var hotList = httpCacheHelper.getCache<List<article_hot>>(hotKey);
                if (hotList == null || hotList.Count == 0)
                {
                    hotList = dbContext.article_hots.Where(x => x.is_show).OrderBy(x => x.sort_num).ThenByDescending(x => x.last_update).Take(12).ToList();
                    httpCacheHelper.setCache(hotKey, hotList, 10);
                }
                ViewData["hot"] = hotList;

                var forecastKey = $"home-index-forecast";
                var forecastList = httpCacheHelper.getCache<List<article_forecast>>(forecastKey);
                if (forecastList == null || forecastList.Count == 0)
                {
                    forecastList = dbContext.art_forecast.Where(x => x.is_show).OrderByDescending(x => x.create_time).Take(12).ToList();
                    httpCacheHelper.setCache(forecastKey, forecastList, 10);
                }
                ViewData["forecast"] = forecastList;  // 默认显示的预测历史


                // 文章分类列表
                var categorieKey = $"home-index-categories";
                var categories = httpCacheHelper.getCache<List<CategoryAricleExt>>(categorieKey);
                if (categories == null || categories.Count == 0)
                {
                    categories = dbContext.art_category.Where(x => x.is_show).OrderBy(x => x.sort).ThenBy(x => x.create_time).Select(x => new CategoryAricleExt() { id = x.id, category_name = x.category_name, img = x.img, sort = x.sort }).Take(6).ToList();
                    if (categories != null && categories.Count > 0)
                    {
                        categories.ForEach(item =>
                        {
                            var artlist = dbContext.article.Where(x => x.cateid == item.id && x.is_show).OrderByDescending(x => x.top).ThenBy(x => x.sort_num).ThenByDescending(x => x.create_time).Take(10).ToList();
                            item.artlist = artlist;
                        });
                    }
                    httpCacheHelper.setCache(categorieKey, categories, 10);
                }

                ViewData["categories"] = categories;
            }
            catch (Exception ex)
            {
                return JsonMsg(ex.Message, ex.StackTrace);
            }

            return View();
        }

        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
